/**
 * Created by IntelliJ IDEA.
 * User: terminator
 * Date: 8/15/11
 * Time: 9:03 AM
 * To change this template use File | Settings | File Templates.
 */
(function(){
    function recover(domainId,value,clickId){
		function removeAny(arr,index){
			for(var i=index;i<arr.length-1;i++){
				arr[i]=arr[i+1];
			}
			arr.length=arr.length-1;
		}
		/*定义除了all这个复选框之外的复选框的点击响应事件*/
		var domain=document.getElementById(domainId);
		var inputList=domain.getElementsByTagName('input'),length=inputList.length;
		var selected=[];
		for(var i=0;i<length;i++){
			if(inputList.value!=value){
				inputList[i].onclick=function(){
					if(this.checked){
						selected.push(this.value);
					}else{
						for(var i=0;i<selected.length;i++){
							if(selected[i]==this.value){
								removeAny(selected,i);
							}
						}
					}
				}
			}
		}
		document.getElementById(clickId).onclick=function(){
			if(this.checked){
				if(value=="recoverAll"){
					for(var i=0;i<length;i++){
						for(var j=0;j<selected.length;j++){
							if(inputList[i].value!=value&&selected[j]==inputList[i].value){
								inputList[i].checked=false;
							}
						}
					}
					for(var k=0;k<length;k++){
						if(inputList[k].value=="recoverImg"||inputList[k].value=="recoverVideo"||inputList[k].value=="recoverAction"){
							inputList[k].checked=false;

						}
						if(inputList[k].value!=value){
							inputList[k].disabled=true;
						}
					}
				}else{
					for(var k=0;k<length;k++){
						if(inputList[k].value!=value){
							inputList[k].disabled=true;
						}
					}
				}

			}
			else{
				if(value=="recoverAll"){
					for(var i=0;i<length;i++){
						for(var j=0;j<selected.length;j++){
							if(inputList[i].value!=value&&selected[j]==inputList[i].value){
								inputList[i].checked=true;
							}
						}
					}

				}
				for(var k=0;k<length;k++){
					inputList[k].disabled=false;
				}
			}
		}
	}
    document.getElementById("ensure").disable=false;
    document.getElementById("ensure").innerHTML='<img src="../resource/images/ensure.png" title="确认" />';
//    function mouseover(){
//        this.style.background="url('../resource/images/content_bg.png') left center repeat-x" ;
//        this.getElementsByTagName('a')[0].style.font="bold 16px \\5fae\\8f6f\\96c5\\9ed1, \\5b8b\\4f53, sans-serif";
//    }
//    function mouseout(){
//        this.style.background="";
//        this.getElementsByTagName('a')[0].style.font="normal 14px \\5fae\\8f6f\\96c5\\9ed1, \\5b8b\\4f53, sans-serif";
//    }
    if(document.getElementById("allAdmin").getElementsByTagName("li").length){
        var scope=document.getElementById("allAdmin").getElementsByTagName("li");
        var length=scope.length;
//        for(var i=0;i<length;i++){
//            scope[i].onmouseover=function(){
//                this.style.background="url('../resource/images/content_bg.png') left center repeat-x" ;
//                this.getElementsByTagName('a')[0].style.font="bold 16px \\5fae\\8f6f\\96c5\\9ed1, \\5b8b\\4f53, sans-serif";
//            } ;
//            scope[i].onmouseout=function(){
//                this.style.background="";
//                this.getElementsByTagName('a')[0].style.font="normal 14px \\5fae\\8f6f\\96c5\\9ed1, \\5b8b\\4f53, sans-serif";
//            } ;
//            scope[i].addEventListener('mouseover',mouseover,true);
//            scope[i].addEventListener('mouseout',mouseout,true);
//        }
        document.getElementById("detail").innerHTML="<p style='color:blue;text-align:center;line-height:170px;font-size:32px;'>请选择要操作的用户</p>";
        var domain=document.getElementById("allAdmin").getElementsByTagName("a");
        var len=domain.length;
        var page=document.getElementById("pageTitle").value;
        if(page=="modify"){
            for(var j=0;j<len;j++){
                domain[j].onclick=function(){
                    this.className="current";
                    for(var s=0;s<length;s++){
                        if(domain[s].innerHTML!=this.innerHTML){
                            domain[s].className="";
                        }
                    }
                    document.getElementById("detail").innerHTML=document.getElementById('detailContent').innerHTML  ;
                    document.getElementById("userName").innerHTML=this.innerHTML;
                    var inputs=document.getElementById("detail").getElementsByTagName("input");
                    for(var n=0;n<inputs.length;n++){
                        inputs[n].disabled=false;
                    }
                    var id = this.id.match(/\d+/);
                    if ( id.length && id[0] )
                    {
                        var url=document.getElementById("userPath").value,data="id="+id[0];
                        sendRequest(url,responseData,data);
                    }

                    document.getElementById("ensure").onclick=function(){
                        var url=document.getElementById("goToUrl").value;
//                        var inputs=document.getElementById("detail").getElementsByTagName("input");
                        for(var i=0;i<inputs.length;i++){
                            if(!inputs[i].disabled){
                                if(inputs[i].checked&&inputs[i].value!="recoverAll"&&inputs[i].value!="recoverImg"&&inputs[i].value!="recoverVideo"&&inputs[i].value!="recoverAction"){
                                    data=data+"&authorList="+inputs[i].value;
                                }
                            }
                        }
                        this.disbled=true;
                        this.innerHTML='<img src="../resource/images/disable_ensure.png" title="disabled"/>';
                        sendRequest(url,isSuccess,data);
                    };

                    recover("detail","recoverAll","recoverAll");
                    recover("image","recoverImg","recoverImg");
                    recover("video","recoverVideo","recoverVideo");
                    recover("action","recoverAction","recoverAction");

                    document.getElementById("reset").onclick=function(){
//                        var inputs=document.getElementById("detail").getElementsByTagName("input");
                        for(var i=0;i<inputs.length;i++){
                            if(inputs[i].checked){
                                inputs[i].checked=false;
                            }
                        }
                        return false;
                    };
                    return false;
                };
            }

        }
        if(page=="assign"){
            for(var k=0;k<len;k++){
                domain[k].onclick=function(){
                    this.className="current";
                    for(var s=0;s<length;s++){
                        if(domain[s].innerHTML!=this.innerHTML){
                            domain[s].className="";
                        }
                    }
                    var id= this.id.match(/\d+/);
                    document.getElementById("detail").innerHTML=document.getElementById('detailContent').innerHTML  ;
                    document.getElementById("userName").innerHTML=this.innerHTML;

                    document.getElementById("ensure").onclick=function(){
                        var url=document.getElementById("goToUrl").value;
                        var data="id="+id;
                        var inputs=document.getElementById("detail").getElementsByTagName("input");
                        for(var i=0;i<inputs.length;i++){
                            if(inputs[i].checked){
                                data=data+"&authorList="+inputs[i].value;
                            }
                        }
                        this.disbled=true;
                        this.innerHTML='<img src="../resource/images/disable_ensure.png" title="disabled"/>';
                        sendRequest(url,isSuccess,data);
                    } ;
                    document.getElementById("reset").onclick=function(){
                        var inputs=document.getElementById("detail").getElementsByTagName("input");
                        for(var i=0;i<inputs.length;i++){
                            if(inputs[i].checked){
                                inputs[i].checked=false;
                            }
                        }
                        return false;
                    };
                };
            }
        }
    }
    else{
        document.getElementById("detail").innerHTML="<p style='color:blue;text-align:center;line-height:170px;font-size:32px;'>暂时没有需要操作的用户</p>";
    }

    /*创建ajax程序*/
    function createXMLHTTP(){
        var xmlHttp=null;
        try{
            xmlHttp=new XMLHttpRequest();
        }catch(e){
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        return xmlHttp;
    }
    function sendRequest(url,callback,postData){
        var xmlHttpObj=createXMLHTTP();
        if(!xmlHttpObj) return;
        var method=(postData)?"POST":"GET";
        xmlHttpObj.open(method,url,true);
//        xmlHttpObj.setRequestHeader('Content-type','XMLHTTP');
        if(postData) xmlHttpObj.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        xmlHttpObj.onreadystatechange=function(){
            if(xmlHttpObj.readyState!=4) return;
            if(xmlHttpObj.status!=200&&xmlHttpObj.status!=304){
                return;
            }
            callback(xmlHttpObj);
        };
        if(xmlHttpObj.readyState==4) return;

        xmlHttpObj.send(postData);
    }
    /*结束ajax程序*/

    function responseData(obj){
        var rpsData=eval(obj.responseText.split(":"));
        var inputs=document.getElementById("detail").getElementsByTagName("input");
        for(var i=0;i<inputs.length;i++){
            for(var j=0;j<rpsData.length;j++){
                if(inputs[i].value==rpsData[j]){
                    inputs[i].checked=true;
                }
            }
        }
    }

    function isSuccess(obj){
        var rpsData=obj.responseText;
        if(rpsData=="true"){
            window.location.reload();
        }else{
            alert("操作失败,请稍后重试");
            document.getElementById("ensure").disable=false;
            document.getElementById("ensure").innerHTML='<img src="../resource/images/ensure.png" title="确认" />';
        }
    }
})();
